//
//  DSZShopViewController.m
//  7.13苏宁框架
//
//  Created by lovena on 15/7/18.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZShopViewController.h"
#import "Common.h"
#import "DSZShowWebViewController.h"
#import "DSZMainViewController.h"
#import "ShopModel.h"
#import "ShowCell.h"
#import "FootDeleView.h"
#import "HeaderView.h"
#import "DSZShopDetailViewController.h"
#import "DSZMainViewController.h"
#import "MJRefresh.h"
#import "MJChiBaoZiHeader.h"
#include "MBProgressHUD+MJ.h"
#import "XQModel.h"
#import "XQViewController.h"
#import "DSZLoginViewController.h"
static const CGFloat MJDuration = 2.0;
@interface DSZShopViewController ()<ShowCellDelegate,FootDeleViewDelegate,UIAlertViewDelegate,HeaderViewDelegate>
@property(nonatomic,retain)NSMutableArray *shopArray;//数组
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIView *secView;//放表的视图
@property(nonatomic,retain)FootView *foot;//底部视图
@property(nonatomic,retain)FootDeleView *deleFoot;//底部删除视图
@property(nonatomic,retain)HeaderView *headerView;//头部视图
@property(nonatomic,strong)UIButton *rightBtn;
@property(nonatomic,retain)DSZMainViewController *main;//分栏控制器
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
- (IBAction)clickLogin;


@end

@implementation DSZShopViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMydata) name:@"login" object:nil];
    
    
    
     _main=(DSZMainViewController *)self.tabBarController;
  
    
    //去逛逛按钮
    self.letgoBtn.layer.cornerRadius=8;
    self.loginBtn.layer.cornerRadius=8;

    //编辑按钮
    _rightBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    _rightBtn.frame=CGRectMake(0, 0, 40, 30);
    [_rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _rightBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    _rightBtn.tag=101;
    [_rightBtn addTarget:self action:@selector(changeVlaue) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc]initWithCustomView:_rightBtn];
    self.navigationItem.rightBarButtonItem=rightBtn;
    

    
        
        DSZShowScroll *show=[DSZShowScroll ShowScroll];
        show.delegate=self;
    if (_main.tabbarView.top==480) {
         show.frame=CGRectMake(0, 300, 320, 180);
    }
    else
    {
        show.frame=CGRectMake(0, 251, 320, 180);
    }
    
        [self.view addSubview:show];
    
}

-(void)reloadMydata
{
    NSString *name;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    name=[defaults objectForKey:@"zh2"];
    if ([name isEqualToString:@"disizu"]) {
        self.loginBtn.hidden=YES;
        self.letgoBtn.hidden=NO;
    }
    else
    {
        
        
        self.letgoBtn.hidden=YES;
        self.loginBtn.hidden=NO;
    }
}

//徽章数修改
-(void)changeBadge:(NSMutableArray *)array
{
    int count=0;
    for (ShopModel *shopmodel in array) {
        count+=shopmodel.count;
    }
    
    //判断
    if (count>=0) {
        [_main loadBadgeView:count];
    }

}
-(void)viewWillAppear:(BOOL)animated
{
    [self reloadMydata];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayTabBar) name:@"displayTabBar" object:nil];
   // 隐藏分栏
    if (_tableView.height==366) {
        [_main hiddenMainTabbar:YES];
    }
   // [_main hiddenMainTabbar:NO];
    self.navigationController.navigationBarHidden=NO;
    
    _shopArray=[NSKeyedUnarchiver unarchiveObjectWithFile:FilePath];
    if (self.shopArray.count>0&&_secView==nil) {
        
        [self initSubTableViews];
        //价钱波动
        [self priceSet];
        [self.tableView reloadData];

        //分栏控制器
        [self changeBadge:_shopArray];
        
        _rightBtn.hidden=NO;
    }
    else
    {
        [self.tableView reloadData];
        [self priceSet];
        [self changeBadge:_shopArray];
    }
     //刷新
    if (_secView!=nil) {
         self.tableView.header = [MJChiBaoZiHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    }
    
    
}
-(void)displayTabBar
{
    [_main hiddenMainTabbar:NO];
}
#pragma mark 下拉刷新数据
- (void)loadNewData
{
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        [MBProgressHUD showSuccess:@"刷新成功"];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"displayTabBar" object:nil];
}
-(NSMutableArray *)shopArray
{
    if (_shopArray==nil) {
        _shopArray=[NSKeyedUnarchiver unarchiveObjectWithFile:FilePath];
       
        if (_shopArray==nil) {
            _shopArray=[NSMutableArray array];
        }
    }
    return _shopArray;
}

//跳到首页
- (IBAction)letgoClick {
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DSZMainViewController *main=[story instantiateViewControllerWithIdentifier:@"main"];
    [self presentViewController: main animated:NO completion:nil];
}


#pragma mark表
//表的创建
-(void)initSubTableViews
{
    _secView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    _secView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_secView];
    if (_main.tabbarView.top==480) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 366)];
    }
    else
    {
         _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 317)];
    }
   
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.sectionHeaderHeight=70;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    [self initFootViews];
    
}
//加载底部视图
-(void)initFootViews
{
    [_deleFoot removeFromSuperview];
    _deleFoot=nil;
    
    //底部视图
    _foot=[FootView CreateFootView];
    _foot.delegate=self;
    _foot.frame=CGRectMake(0, _tableView.bottom, 320, 50);
    [self.secView addSubview:_foot];
    
    //底部滑动
    DSZShowScroll *show=[DSZShowScroll ShowScroll];
    show.delegate=self;
    self.tableView.tableFooterView=show;
    [_secView addSubview:_tableView];

 
}

//表的
-(void)initDeleFoots
{
    //首先把表的表尾去掉
    _tableView.tableFooterView=nil;
    
    //然后吧底部视图换为删除视图
    [_foot removeFromSuperview];
    _foot=nil;
    _deleFoot=[FootDeleView CreateDeleFoot];
    _deleFoot.delegate=self;
    _deleFoot.frame=CGRectMake(0, _tableView.bottom, 320, 50);
    [self.secView addSubview:_deleFoot];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // NSLog(@"%i",self.shopArray.count);
    return self.shopArray.count;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=@"cell";
    ShowCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[ShowCell CreateShopCell];
    }
    cell.delegate=self;
    //两个按钮tag
    cell.subBtn.tag=indexPath.row;
    cell.addBtn.tag=indexPath.row;
    cell.selectedBtn.tag=indexPath.row;

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.smodel=self.shopArray[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
//段头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //头部
    _headerView=[HeaderView createHeader];
    _headerView.frame=CGRectMake(0, 0, 320, 70);
    _headerView.delegate=self;
    
    //偏好设置类，存放头部相关设置
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    _headerView.selectedBtn.selected=[defaults boolForKey:@"btn"];
    _headerView.carriage.text=[defaults objectForKey:@"carriage"];
    
    return _headerView;
   

}


//代理
-(void)giveModel:(ShowScrollModel *)model
{
    DSZShowWebViewController *showweb=[[DSZShowWebViewController alloc]init];
    showweb.showModel=model;
    [self.navigationController pushViewController:showweb animated:YES];
}
#pragma mark编辑按钮
-(void)changeVlaue
{
   // 判断是否有商品
    if(_secView!=nil)
    {
        _rightBtn.hidden=NO;
    }
    else
        _rightBtn.hidden=YES;
    
    
    
    if(_rightBtn.tag==101)
    {
        //表子为完成
        [_rightBtn setTitle:@"完成" forState:UIControlStateNormal];
        
        [self initDeleFoots];
        
        //进入编辑之后，单元格所有的选中按钮都不选中
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=NO;
        }
        _headerView.selectedBtn.selected=NO;
        //刷表
        [self.tableView reloadData];
        //分栏控制器
        [self changeBadge:_shopArray];
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
        
        //改变表的tag
        _rightBtn.tag=102;
        
        //删除按钮不可用
        _deleFoot.deleBtn.enabled=NO;
    }
    else
    {
        [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
        //把数组中所有单元格改为选中
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=YES;
        }
        
        [self initFootViews];
        //底部视图的选中按钮为选中
        _foot.selectedBtn.selected=YES;
        //刷表
        _headerView.selectedBtn.selected=YES;
        [self.tableView reloadData];
        [self priceSet];
        //分栏控制器
        [self changeBadge:_shopArray];
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
        
        _rightBtn.tag=101;
    }
    
    //偏好设置
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setBool:_headerView.selectedBtn.selected forKey:@"btn"];
    [defaults synchronize];
}

#pragma mark头部代理
-(void)headerSelected:(UIButton *)btn
{
    
    if (btn.selected) {
        btn.selected=NO;
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=NO;
        }
        //底部视图全选按钮
        
        if (_deleFoot!=nil) {
            //删除按钮不可用
            _deleFoot.deleBtn.enabled=NO;
        }
        
    }
    else
    {
        btn.selected=YES;
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=YES;
        }
        if (_deleFoot!=nil) {
            //删除按钮可用
            _deleFoot.deleBtn.enabled=YES;
        }
    }
    
    
    //判断两个按钮之间的关系
    if(_foot!=nil)
    {
        _foot.selectedBtn.selected=btn.selected;
    }
    if (_deleFoot!=nil) {
        _deleFoot.deleSelectedBtn.selected=btn.selected;
    }

    
    [self.tableView reloadData];
    [self priceSet];
    
    //偏好设置
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setBool:btn.selected forKey:@"btn"];
    [defaults synchronize];

    //分栏控制器
    [self changeBadge:_shopArray];
    [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
    
    
    
}
#pragma mark底部删除视图代理
//全选按钮
-(void)deleSelectedBtn:(UIButton *)btn
{
    //判断是否全选
    if (btn.selected)//如果出于全选状态
    {
        btn.selected=NO;
        
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=NO;
        }
        //底部删除个数
        _deleFoot.countLabel.text=@"(已选:0)";
        
        //删除按钮不可用
        _deleFoot.deleBtn.enabled=NO;
       
    }
    else
    {
        btn.selected=YES;
        int count=0;
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=YES;
            count+=shopmodel.count;
        }
        //底部删除个数
        _deleFoot.countLabel.text=[NSString stringWithFormat:@"(已选:%i)",count];
        //删除按钮可用
        _deleFoot.deleBtn.enabled=YES;
    }
    //刷新
    [self.tableView reloadData];
    //分栏控制器
    [self changeBadge:_shopArray];
    [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
    
    //判断两个按钮之间的关系
    if(_deleFoot!=nil)
    {
        _headerView.selectedBtn.selected=btn.selected;
        //偏好设置
        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setBool:_headerView.selectedBtn.selected forKey:@"btn"];
        [defaults synchronize];
    }
    
}
//删除按钮
-(void)deleGOtoBtn:(UIButton *)btn
{
    //提醒框
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"确定删除购物车选中商品?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
    

    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        //如果选中了，就要删除
        //创建一个新的数组房没有选中的单元格
        NSMutableArray *newArray=[NSMutableArray array];
        for (ShopModel *shopmodel in _shopArray) {
            if (shopmodel.flag==NO) {
                [newArray addObject:shopmodel];
            }
        }
        
        
        //把最新的数组传给根数组
        _shopArray=newArray;
        //分栏控制器
        [self changeBadge:_shopArray];
        
        //判断是否用表
        if (self.shopArray.count<=0) {
            [_secView removeFromSuperview];
            _secView=nil;
            [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
            //编辑按钮返回编辑
            [self changeVlaue];
            return;
        }
        

        
        //刷表
        [self.tableView reloadData]; 
        


        
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
    }
}
#pragma mark代理
-(void)addBuys2:(ShopModel *)model2
{
    if (_loginBtn.hidden) {
        //加载表视图
        
        BOOL isHas=NO;
        //判断数组内是否有此商品
        //有的情况
        for(int i=0;i<self.shopArray.count;i++)
        {
            ShopModel *shopmodel2=self.shopArray[i];
            if ([shopmodel2.name isEqualToString:model2.name]) {
                shopmodel2.count+=1;
                isHas=YES;
                continue;
            }
        }
        if (isHas==NO)
        {
            [_shopArray addObject:model2];
        }
        //刷新
        
        //判断是否用表
        if (self.shopArray.count>0&&_secView==nil) {
            
            [self initSubTableViews];
            [self.tableView reloadData];
            
            
            _rightBtn.hidden=NO;
        }
        //分栏控制器
        [self changeBadge:_shopArray];
        
        [self.tableView reloadData];
        [self priceSet];
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];

    }
    else
    {
        [MBProgressHUD showError:@"先去登录一下吧"];
    }
   }

//底部代理
//选中按钮
-(void)didSelectedBtn:(UIButton *)btn
{
    
    
    //如果他选中，则全部被选中，不选中，全部不选中
    //如果他选中
    if (btn.selected) {
        //让他不被选中
        btn.selected=NO;
        //所有单元格不悲选中
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=NO;
        }
    }
    else
    {
        btn.selected=YES;
        for (ShopModel *shopmodel in _shopArray) {
            shopmodel.flag=YES;
        }
    }
    //刷表
    [self.tableView reloadData];
    [self priceSet];
    //分栏控制器
    [self changeBadge:_shopArray];
    [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
    
    //判断两个按钮之间的关系
    if(_foot!=nil)
    {
        _headerView.selectedBtn.selected=btn.selected;
        //偏好设置
        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setBool:_headerView.selectedBtn.selected forKey:@"btn"];
        [defaults synchronize];
    }

}
#pragma mark核心操作
-(void)priceSet
{
    //付款按钮状态
    _foot.payMoney.selected=NO;
    _foot.payMoney.enabled=YES;
    
    
    
    int allCount=0;//有几件
    float allprice=0;//价钱
    //原来价钱
    float oldprice=0;
    //如果全部为选中，直接判断底部按钮是否为选中
    //如果底部按钮没有选中，在逐一判断单元格选中按钮
    if (_foot.selectedBtn.selected) {
        //直接把所有的价钱加起来就行
        for (ShopModel *shopmodel in _shopArray) {
            allCount+=shopmodel.count;//这里的件是所有的包括同一类多个
            float price=[shopmodel.price floatValue];
            allprice+=price*shopmodel.count;
            float old=[shopmodel.oldprice floatValue];
            oldprice+=old*shopmodel.count;
        }
    }
    else
    {
        //判断
        for (ShopModel *shopmodel in _shopArray) {
            if (shopmodel.flag) {
                allCount+=shopmodel.count;//这里的件是所有的包括同一类多个
                float price=[shopmodel.price floatValue];
                allprice+=price*shopmodel.count;
                float old=[shopmodel.oldprice floatValue];
                oldprice+=old*shopmodel.count;
            }
        }
    }
    
    //得出结果
    _foot.allPrice.text=[NSString stringWithFormat:@"¥%.2f",allprice];
    NSString *str=[NSString stringWithFormat:@"去结算(%i)",allCount];
    [_foot.payMoney setTitle:str forState:UIControlStateNormal];
    _foot.marginPrice.text=[NSString stringWithFormat:@"已优惠%.2f",oldprice-allprice];

    //判断是否需要运费
    BOOL iscarr=NO;
    for ( ShopModel *shopmodel in _shopArray) {
        if (shopmodel.flag) {
            iscarr=YES;
            continue;
        }
    }
     NSString *carr=@"";//用于头部的运费
    if (iscarr) {
        if (allprice>=69) {
            carr=@"免运费";
            self.foot.carriage.text=@"免运费";
        }
        else
        {
            carr=@"运费:5元";
            _foot.carriage.text=@"运费:5元";
        }
       // NSLog(@"%@",_headerView.carriage.text);
    }
    else
    {
        carr=@"";
        _foot.carriage.text=@"";
    }
   //存放头部运费信息
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:carr forKey:@"carriage"];
    [defaults synchronize];
    
    
    //判断一次付款按钮的状态，是否可用，如果一件物品没有或者一件也没有选时，不可用
    //首先判断是否为空
    if (_shopArray.count<=0) {
        _foot.payMoney.selected=YES;
        [_foot.payMoney setTitle:@"去结算" forState:UIControlStateSelected];
        _foot.payMoney.enabled=NO;
        //背景图片
        return;
    }
    else
    {
        BOOL iscan=NO;
        for (ShopModel *showmodel in _shopArray) {
            if(showmodel.flag==YES)
            {
                iscan=YES;
                continue;
            }
        }
        //判断
        if (iscan==NO) {
            _foot.payMoney.selected=YES;
            _foot.payMoney.enabled=NO;
            [_foot.payMoney setTitle:@"去结算" forState:UIControlStateNormal];
            
            //背景图片
        }
        
     }
    
       BOOL isall=YES;
    //判断底部全选按钮的状态
    for (ShopModel *shopmmodel in _shopArray) {
        if (shopmmodel.flag==NO) {
            isall=NO;
            continue;
        }
    }
    //底部全选的按钮的状态
    if (isall) {
        _foot.selectedBtn.selected=YES;
    }
    else
    {
        _foot.selectedBtn.selected=NO;
    }
    
    
    if (_deleFoot!=nil) {
        int deleCount=0;//底部删除几个
        for (ShopModel *shopmodel in _shopArray) {
            if (shopmodel.flag) {
                deleCount+=shopmodel.count;
            }
        }
        
        _deleFoot.countLabel.text=[NSString stringWithFormat:@"已选:%i",deleCount];
    }
    
    

    
}


//付款按钮
-(void)didPaySelectedBtn:(UIButton *)btn
{
//／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／／
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"ShopDetailStory" bundle:nil];
    DSZShopDetailViewController *detail=[story instantiateViewControllerWithIdentifier:@"showdetail"];
    [self.navigationController pushViewController:detail animated:YES];
}


//单元格代理
//减少
-(void)shopSubCount:(int)index
{
    
    //首先判断个数是不是大于1
    ShopModel *shopmodel=self.shopArray[index];
    if(shopmodel.count>1)
    {
        shopmodel.count-=1;
    //刷表
        [self.tableView reloadData];
        //底部价钱波动
        [self priceSet];
        //分栏控制器
        [self changeBadge:_shopArray];
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
    }
   
    
}
//增加
-(void)shopAddCount:(int)index
{
    ShopModel *shopmodel=self.shopArray[index];
        shopmodel.count+=1;
        //刷表
        [self.tableView reloadData];
         //底部价钱波动
         [self priceSet];
        //分栏控制器
        [self changeBadge:_shopArray];
        [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];

}
//选中按钮
-(void)shopSelected:(UIButton *)btn
{
    //判断是否是出于选中状态
     ShopModel *shopmodel=self.shopArray[btn.tag];
    if (shopmodel.flag==YES) {
        //变为不选中状态
        shopmodel.flag=NO;
        //图片改变
        btn.selected=NO;
    }
    else
    {
        shopmodel.flag=YES;
        btn.selected=YES;
    }
    
    
    //底部为价钱时
    //底部选中按钮的状态
    //如果全部订单呗选中，他为选中，否则为不选中
    if(_foot!=nil)
    {
        BOOL isall=YES;
        for (ShopModel *shop in _shopArray) {
            //如果有一个不选中
            if (shop.flag==NO) {
                isall=NO;
                continue;
            }
        }
        //判断底部按钮状态
        if (isall) {
            _foot.selectedBtn.selected=YES;
        }
        
        else
        {
            _foot.selectedBtn.selected=NO;
        }
        
        [self.tableView reloadData];
       
 
    }
    else//判断底部删除视图全选按钮
    {
        BOOL isall=YES;
        for (ShopModel *shop in _shopArray) {
            //如果有一个不选中
            if (shop.flag==NO) {
                isall=NO;
                continue;
            }
        }
        //判断底部按钮状态
        if (isall) {
            _deleFoot.deleSelectedBtn.selected=YES;
            
        }
        else
        {
            _deleFoot.deleSelectedBtn.selected=NO;
        }
        
        [self.tableView reloadData];
    }
    
   
    BOOL isheader=YES;
    for (ShopModel *shop in _shopArray) {
        //如果有一个不选中
        if (shop.flag==NO) {
            isheader=NO;
            continue;
        }
    }
    //判断底部按钮状态
    if (isheader) {
        _headerView.selectedBtn.selected=YES;
    }
    
    else
    {
         _headerView.selectedBtn.selected=NO;
    }

//    //底部删除个数
//    int delecount=0;
//    if (_deleFoot!=nil) {
//        for (ShopModel *shop in _shopArray) {
//            if (shop.flag==YES) {
//                delecount+=1;
//            }
//        }
//        //底部删除个数
//    _deleFoot.countLabel.text=[NSString stringWithFormat:@"(已选:%i)",delecount];
//    }
    
    //判断底部删除按钮的可用性
    BOOL isenable=NO;
    for (ShopModel *shopmodel in _shopArray) {
        if (shopmodel.flag==YES) {
            isenable=YES;
            continue;
        }
    }
    _deleFoot.deleBtn.enabled=isenable;
    //底部价钱波动
    [self priceSet];
    //偏好设置
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setBool:_headerView.selectedBtn.selected forKey:@"btn"];
    [defaults synchronize];
    //分栏控制器
    [self changeBadge:_shopArray];
    
      [NSKeyedArchiver archiveRootObject:_shopArray toFile:FilePath];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopModel *model = _shopArray[indexPath.row];
    XQModel *xqmodel = [[XQModel alloc]init];
    xqmodel.image = model.image;
    xqmodel.name = model.name;
    xqmodel.price = model.price;
    xqmodel.oldprice = model.oldprice;
    xqmodel.know = model.know;
    xqmodel.fuwu = model.fuwu;
    xqmodel.hpl = model.hpl;
    xqmodel.person = model.person;
    xqmodel.pj = model.pj;
    xqmodel.detailpj = model.detailpj;
    xqmodel.smallimage = model.smallimage;
    xqmodel.type = model.type;
    //把模型对象传给控制器，让控制器跳转到商品详情页面
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"XQ" bundle:nil];
    XQViewController *con = [story instantiateViewControllerWithIdentifier:@"xq"];
    con.model = xqmodel;
    [self.navigationController pushViewController:con animated:YES];

    
}
#pragma mark--登录
- (IBAction)clickLogin {
    DSZLoginViewController *login=[[DSZLoginViewController alloc]init];
    [self.navigationController pushViewController:login animated:NO];
}
@end
